package com.xwb.laboratory.test.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyDemo {

	public String test1() {
		//被代理的对象
		ProgrammerX x = new ProgrammerX();
		
		//要增强的功能
		InvocationHandler i = new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				String s1 = "环境配置检查\n";
				String s2 = "\n代码规范检查";
				String r = (String) method.invoke(x, args);
				return s1 + r + s2;
			}
		};
		
		//创建代理对象
		Programmer p = (Programmer)Proxy.newProxyInstance(
				x.getClass().getClassLoader(), 
				x.getClass().getInterfaces(), 
				i);
		
		String code = p.code();
		
		return code;
		
	}
	
	public static void main(String[] args) {
		ProxyDemo pd = new ProxyDemo();
		System.out.println(pd.test1());
	}
}
